package io.dy.custom;
import io.dy.utils.ThreadUtils;
import java.util.concurrent.*;

/**
 * @author : dmj
 * @date: 2024/5/20 4:20 下午
 * @description:
 */
public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

    public CustomThreadPoolExecutor(int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    ThreadFactory threadFactory,
                                    RejectedExecutionHandler handler) {

        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        //处理异常
        if (t != null) {
            //表示execute提交方式抛出的异常
            ThreadUtils.printThreadLog("afterExecute里面获取到execute提交的异常信息，处理异常信息:" + t.getMessage());
            //可以记录异常信息。。。
        }

        //判断 r 是否是 FutureTask 类型； 若是 表示submit方式提交
        if(r instanceof FutureTask){
            try {
                Future<?> future = (Future<?>) r;
                //submit提交异常信息 必须通过get获取结果的时候才会抛出
                future.get();
            } catch (Exception e) {
                ThreadUtils.printThreadLog("afterExecute里面获取到execute提交的异常信息，处理异常信息:" + e);
                //可以记录异常信息。。。
            }
        }
    }
}
